<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:text="${product.productName} + ' - 商品详情'">商品详情 - 商品商城</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4e73df;
            --secondary-color: #858796;
            --success-color: #1cc88a;
            --info-color: #36b9cc;
            --warning-color: #f6c23e;
            --danger-color: #e74a3b;
            --light-color: #f8f9fc;
            --dark-color: #5a5c69;
        }

        body {
            font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fc;
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
        }

        .breadcrumb {
            background-color: transparent;
            padding: 1rem 0;
        }

        .product-detail-container {
            background-color: white;
            border-radius: 0.5rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            padding: 2rem;
            margin-bottom: 2rem;
        }

        /* 商品图片轮播样式 */
        .product-image-carousel {
            position: relative;
            overflow: hidden;
            border-radius: 0.5rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            margin-bottom: 1rem;
        }

        .carousel-item img {
            width: 100%;
            height: 400px;
            object-fit: contain;
            cursor: pointer;
        }

        .carousel-control-prev, .carousel-control-next {
            width: 40px;
            background-color: rgba(0, 0, 0, 0.3);
        }

        .carousel-indicators button {
            width: 10px;
            height: 10px;
            border-radius: 50%;
        }

        /* 商品缩略图样式 */
        .product-thumbnails {
            display: flex;
            gap: 10px;
            margin-top: 1rem;
        }

        .thumbnail {
            width: 80px;
            height: 80px;
            border: 2px solid transparent;
            border-radius: 0.25rem;
            cursor: pointer;
            transition: all 0.3s ease;
            object-fit: cover;
        }

        .thumbnail:hover, .thumbnail.active {
            border-color: var(--primary-color);
        }

        .badge-hot {
            background-color: var(--danger-color);
        }

        .badge-new {
            background-color: var(--success-color);
        }

        .product-price {
            font-weight: 700;
            color: var(--danger-color);
            font-size: 2rem;
        }

        .product-original-price {
            text-decoration: line-through;
            color: var(--secondary-color);
            font-size: 1.2rem;
        }

        .product-info-item {
            padding: 0.75rem 0;
            border-bottom: 1px solid #e3e6f0;
        }

        .product-info-item:last-child {
            border-bottom: none;
        }

        .product-info-label {
            font-weight: 600;
            color: var(--secondary-color);
            width: 100px;
        }

        .quantity-selector {
            display: flex;
            align-items: center;
        }

        .quantity-selector .form-control {
            width: 80px;
            text-align: center;
            margin: 0 0.5rem;
        }

        .action-buttons .btn {
            margin-right: 1rem;
        }

        .related-products {
            background-color: white;
            border-radius: 0.5rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            padding: 2rem;
        }

        .related-product-card {
            border: none;
            border-radius: 0.5rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            transition: all 0.3s ease;
            margin-bottom: 1.5rem;
            overflow: hidden;
        }

        .related-product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0.25rem 2rem 0 rgba(58, 59, 69, 0.2);
        }

        .related-product-card .card-img-top {
            height: 150px;
            object-fit: cover;
            transition: transform 0.3s ease;
        }

        .related-product-card:hover .card-img-top {
            transform: scale(1.05);
        }

        .related-product-price {
            font-weight: 700;
            color: var(--danger-color);
        }

        .section-title {
            position: relative;
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }

        .section-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 50px;
            height: 3px;
            background-color: var(--primary-color);
        }

        .stock-status {
            padding: 0.25rem 0.75rem;
            border-radius: 1rem;
            font-size: 0.875rem;
            font-weight: 600;
        }

        .stock-normal {
            background-color: rgba(28, 200, 138, 0.1);
            color: var(--success-color);
        }

        .stock-low {
            background-color: rgba(246, 194, 62, 0.1);
            color: var(--warning-color);
        }

        .stock-out {
            background-color: rgba(231, 74, 59, 0.1);
            color: var(--danger-color);
        }

        .product-description {
            white-space: pre-wrap;
        }

        .nav-tabs .nav-link {
            color: var(--secondary-color);
            border: none;
            border-bottom: 2px solid transparent;
            padding: 0.75rem 1.5rem;
        }

        .nav-tabs .nav-link.active {
            color: var(--primary-color);
            background-color: transparent;
            border-bottom: 2px solid var(--primary-color);
        }

        .tab-content {
            padding: 1.5rem 0;
        }

        /* 图片放大查看模态框样式 */
        .image-modal .modal-dialog {
            max-width: 90%;
        }

        .image-modal .modal-body {
            padding: 0;
        }

        .image-modal img {
            width: 100%;
            height: auto;
        }

        /* 收藏按钮样式 */
        .btn-favorite {
            transition: all 0.3s ease;
        }

        .btn-favorite.active {
            color: var(--danger-color);
            background-color: rgba(231, 74, 59, 0.1);
            border-color: var(--danger-color);
        }

        /* 分享按钮样式 */
        .share-buttons {
            display: flex;
            gap: 10px;
            margin-top: 15px;
        }

        .share-button {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            transition: all 0.3s ease;
        }

        .share-button:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .share-wechat { background-color: #07C160; }
        .share-weibo { background-color: #E6162D; }
        .share-qq { background-color: #12B7F5; }
        .share-link { background-color: #6c757d; }

        /* 评价样式 */
        .review-item {
            border-bottom: 1px solid #e3e6f0;
            padding-bottom: 1.5rem;
            margin-bottom: 1.5rem;
        }

        .review-item:last-child {
            border-bottom: none;
            margin-bottom: 0;
        }

        .review-rating {
            color: #ffc107;
        }

        .review-avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            object-fit: cover;
        }

        .review-images {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }

        .review-image {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 0.25rem;
            cursor: pointer;
        }

        /* 库存提醒样式 */
        .stock-alert {
            background-color: rgba(246, 194, 62, 0.1);
            border: 1px solid var(--warning-color);
            border-radius: 0.25rem;
            padding: 0.75rem;
            margin-bottom: 1rem;
            color: var(--warning-color);
        }

        /* 商品促销标签 */
        .promotion-tag {
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: var(--danger-color);
            color: white;
            padding: 0.25rem 0.5rem;
            border-radius: 0.25rem;
            font-size: 0.875rem;
            font-weight: 600;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .carousel-item img {
                height: 250px;
            }
            
            .product-price {
                font-size: 1.5rem;
            }
            
            .action-buttons .btn {
                margin-bottom: 0.5rem;
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm sticky-top">
        <div class="container">
            <a class="navbar-brand" href="/shop">
                <i class="bi bi-shop"></i> 商品商城
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/shop">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/products">所有商品</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/categories">商品分类</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/promotions">促销商品</a>
                    </li>
                </ul>
                <form class="d-flex me-3" action="/shop/search" method="get">
                    <input class="form-control me-2" type="search" name="keyword" placeholder="搜索商品...">
                    <button class="btn btn-outline-primary" type="submit">
                        <i class="bi bi-search"></i>
                    </button>
                </form>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i> 我的账户
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="/auth/login">登录</a></li>
                            <li><a class="dropdown-item" href="/auth/register">注册</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="/shop/profile">个人中心</a></li>
                            <li><a class="dropdown-item" href="/shop/orders">我的订单</a></li>
                            <li><a class="dropdown-item" href="/shop/cart">购物车</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 面包屑导航 -->
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="/shop">首页</a></li>
                <li class="breadcrumb-item"><a href="/shop/products">所有商品</a></li>
                <li class="breadcrumb-item"><a th:href="@{/shop/category(id=${product.categoryId})}" th:text="${product.categoryName}">分类名称</a></li>
                <li class="breadcrumb-item active" aria-current="page" th:text="${product.productName}">商品名称</li>
            </ol>
        </nav>
    </div>

    <!-- 商品详情 -->
    <div class="container">
        <div class="product-detail-container">
            <div class="row">
                <!-- 商品图片 -->
                <div class="col-md-6">
                    <!-- 商品图片轮播 -->
                    <div id="productImageCarousel" class="carousel slide product-image-carousel" data-bs-ride="carousel">
                        <div class="carousel-indicators">
                            <button type="button" data-bs-target="#productImageCarousel" data-bs-slide-to="0" class="active"></button>
                            <!-- 如果有更多图片，可以在这里添加更多指示器 -->
                        </div>
                        <div class="carousel-inner">
                            <div class="carousel-item active">
                                <img th:src="${product.imageUrl ?: '/images/product-placeholder.jpg'}" class="d-block w-100 product-main-image" th:alt="${product.productName}" data-bs-toggle="modal" data-bs-target="#imageModal">
                            </div>
                            <!-- 如果有更多图片，可以在这里添加更多轮播项 -->
                        </div>
                        <button class="carousel-control-prev" type="button" data-bs-target="#productImageCarousel" data-bs-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                            <span class="visually-hidden">上一个</span>
                        </button>
                        <button class="carousel-control-next" type="button" data-bs-target="#productImageCarousel" data-bs-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                            <span class="visually-hidden">下一个</span>
                        </button>
                    </div>

                    <!-- 商品缩略图 -->
                    <div class="product-thumbnails">
                        <img th:src="${product.imageUrl ?: '/images/product-placeholder.jpg'}" class="thumbnail active" th:alt="${product.productName}" data-bs-target="#productImageCarousel" data-bs-slide-to="0">
                        <!-- 如果有更多图片，可以在这里添加更多缩略图 -->
                    </div>
                </div>

                <!-- 商品信息 -->
                <div class="col-md-6">
                    <h1 class="mb-3" th:text="${product.productName}">商品名称</h1>
                    
                    <div class="mb-3">
                        <span class="product-price" th:text="'¥' + ${#numbers.formatDecimal(product.salePrice, 1, 2)}">¥99.99</span>
                        <span th:if="${product.salePrice < product.purchasePrice * 1.2}" class="product-original-price ms-2" th:text="'¥' + ${#numbers.formatDecimal(product.purchasePrice * 1.2, 1, 2)}">¥129.99</span>
                    </div>

                    <div class="mb-3">
                        <span th:if="${product.stockStatus == '正常'}" class="stock-status stock-normal" th:text="${product.stockStatus}">库存正常</span>
                        <span th:if="${product.stockStatus == '不足'}" class="stock-status stock-low" th:text="${product.stockStatus}">库存不足</span>
                        <span th:if="${product.stockStatus == '缺货'}" class="stock-status stock-out" th:text="${product.stockStatus}">缺货</span>
                        <span class="ms-2 text-muted" th:text="'库存: ' + ${product.stockQuantity} + ' ' + ${product.unit}">库存: 100 件</span>
                    </div>

                    <!-- 库存不足提醒 -->
                    <div th:if="${product.stockStatus == '不足'}" class="stock-alert">
                        <i class="bi bi-exclamation-triangle me-2"></i>
                        <span th:text="'库存仅剩 ' + ${product.stockQuantity} + ' ' + ${product.unit} + '，请尽快下单！'">库存仅剩 10 件，请尽快下单！</span>
                    </div>

                    <div class="product-info-item">
                        <div class="d-flex">
                            <span class="product-info-label">商品编码:</span>
                            <span th:text="${product.productCode}">商品编码</span>
                        </div>
                    </div>

                    <div class="product-info-item">
                        <div class="d-flex">
                            <span class="product-info-label">商品分类:</span>
                            <span th:text="${product.categoryName}">分类名称</span>
                        </div>
                    </div>

                    <div class="product-info-item" th:if="${product.brand != null and product.brand != ''}">
                        <div class="d-flex">
                            <span class="product-info-label">品牌:</span>
                            <span th:text="${product.brand}">品牌名称</span>
                        </div>
                    </div>

                    <div class="product-info-item" th:if="${product.specification != null and product.specification != ''}">
                        <div class="d-flex">
                            <span class="product-info-label">规格:</span>
                            <span th:text="${product.specification}">规格参数</span>
                        </div>
                    </div>

                    <div class="product-info-item">
                        <div class="d-flex">
                            <span class="product-info-label">销量:</span>
                            <span th:text="${product.salesCount} + ' ' + ${product.unit}">0 件</span>
                        </div>
                    </div>

                    <div class="product-info-item">
                        <div class="d-flex">
                            <span class="product-info-label">浏览次数:</span>
                            <span th:text="${product.viewCount}">0 次</span>
                        </div>
                    </div>

                    <!-- 购买数量选择 -->
                    <div class="mb-4">
                        <div class="d-flex align-items-center">
                            <span class="me-3">数量:</span>
                            <div class="quantity-selector">
                                <button class="btn btn-outline-secondary" type="button" id="decreaseBtn">
                                    <i class="bi bi-dash"></i>
                                </button>
                                <input type="number" class="form-control" id="quantityInput" value="1" min="1" th:max="${product.stockQuantity}">
                                <button class="btn btn-outline-secondary" type="button" id="increaseBtn">
                                    <i class="bi bi-plus"></i>
                                </button>
                            </div>
                        </div>
                    </div>

                    <!-- 操作按钮 -->
                    <div class="action-buttons">
                        <button class="btn btn-danger btn-lg" id="addToCartBtn" th:disabled="${product.stockQuantity <= 0}">
                            <i class="bi bi-cart-plus me-2"></i>加入购物车
                        </button>
                        <button class="btn btn-outline-danger btn-lg" id="buyNowBtn" th:disabled="${product.stockQuantity <= 0}">
                            <i class="bi bi-lightning me-2"></i>立即购买
                        </button>
                        <button class="btn btn-outline-secondary btn-lg btn-favorite" id="favoriteBtn">
                            <i class="bi bi-heart me-2"></i>收藏
                        </button>
                    </div>

                    <!-- 分享按钮 -->
                    <div class="share-buttons">
                        <button class="share-button share-wechat" title="分享到微信">
                            <i class="bi bi-wechat"></i>
                        </button>
                        <button class="share-button share-weibo" title="分享到微博">
                            <i class="bi bi-sina-weibo"></i>
                        </button>
                        <button class="share-button share-qq" title="分享到QQ">
                            <i class="bi bi-tencent-qq"></i>
                        </button>
                        <button class="share-button share-link" id="copyLinkBtn" title="复制链接">
                            <i class="bi bi-link-45deg"></i>
                        </button>
                    </div>
                </div>
            </div>

            <!-- 商品详情标签页 -->
            <div class="mt-5">
                <ul class="nav nav-tabs" id="productTab" role="tablist">
                    <li class="nav-item" role="presentation">
                        <button class="nav-link active" id="description-tab" data-bs-toggle="tab" data-bs-target="#description" type="button" role="tab">商品描述</button>
                    </li>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" id="details-tab" data-bs-toggle="tab" data-bs-target="#details" type="button" role="tab">详细参数</button>
                    </li>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" id="reviews-tab" data-bs-toggle="tab" data-bs-target="#reviews" type="button" role="tab">用户评价</button>
                    </li>
                </ul>
                <div class="tab-content" id="productTabContent">
                    <div class="tab-pane fade show active" id="description" role="tabpanel">
                        <div class="product-description" th:text="${product.description ?: '暂无商品描述'}">商品描述内容</div>
                    </div>
                    <div class="tab-pane fade" id="details" role="tabpanel">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td class="text-muted">商品编码:</td>
                                        <td th:text="${product.productCode}">商品编码</td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">商品分类:</td>
                                        <td th:text="${product.categoryName}">分类名称</td>
                                    </tr>
                                    <tr th:if="${product.brand != null and product.brand != ''}">
                                        <td class="text-muted">品牌:</td>
                                        <td th:text="${product.brand}">品牌名称</td>
                                    </tr>
                                    <tr th:if="${product.specification != null and product.specification != ''}">
                                        <td class="text-muted">规格:</td>
                                        <td th:text="${product.specification}">规格参数</td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td class="text-muted">单位:</td>
                                        <td th:text="${product.unit}">单位</td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">库存数量:</td>
                                        <td th:text="${product.stockQuantity} + ' ' + ${product.unit}">库存数量</td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">销量:</td>
                                        <td th:text="${product.salesCount} + ' ' + ${product.unit}">销量</td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">上架时间:</td>
                                        <td th:text="${#temporals.format(product.createdAt, 'yyyy-MM-dd')}">上架时间</td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="reviews" role="tabpanel">
                        <div class="text-center py-5">
                            <i class="bi bi-star fs-1 text-muted"></i>
                            <h4 class="mt-3">暂无用户评价</h4>
                            <p class="text-muted">成为第一个评价此商品的用户</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 相关商品推荐 -->
        <div class="related-products" th:if="${relatedProducts != null and !#lists.isEmpty(relatedProducts)}">
            <h3 class="section-title">相关推荐</h3>
            <div class="row">
                <div class="col-md-3 col-sm-6" th:each="product : ${relatedProducts}">
                    <div class="card related-product-card">
                        <div class="position-relative">
                            <img th:src="${product.imageUrl ?: '/images/product-placeholder.jpg'}" class="card-img-top" th:alt="${product.productName}">
                            <div class="position-absolute top-0 end-0 p-2">
                                <span th:if="${product.isHot}" class="badge badge-hot">热销</span>
                                <span th:if="${product.isNew}" class="badge badge-new">新品</span>
                            </div>
                        </div>
                        <div class="card-body">
                            <h5 class="card-title" th:text="${product.productName}">商品名称</h5>
                            <p class="card-text text-muted small" th:text="${product.categoryName}">分类名称</p>
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="related-product-price" th:text="'¥' + ${#numbers.formatDecimal(product.salePrice, 1, 2)}">¥99.99</span>
                                <a th:href="@{/shop/product/{id}(id=${product.id})}" class="btn btn-sm btn-primary">查看详情</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 图片放大查看模态框 -->
    <div class="modal fade image-modal" id="imageModal" tabindex="-1" aria-labelledby="imageModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="imageModalLabel" th:text="${product.productName}">商品图片</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <img th:src="${product.imageUrl ?: '/images/product-placeholder.jpg'}" th:alt="${product.productName}">
                </div>
            </div>
        </div>
    </div>

    <!-- 加入购物车成功提示 -->
    <div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
        <div id="cartToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="toast-header">
                <i class="bi bi-check-circle-fill text-success me-2"></i>
                <strong class="me-auto">提示</strong>
                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body">
                商品已成功加入购物车！
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const decreaseBtn = document.getElementById('decreaseBtn');
            const increaseBtn = document.getElementById('increaseBtn');
            const quantityInput = document.getElementById('quantityInput');
            const maxQuantity = parseInt(quantityInput.getAttribute('max'));
            const addToCartBtn = document.getElementById('addToCartBtn');
            const buyNowBtn = document.getElementById('buyNowBtn');
            const favoriteBtn = document.getElementById('favoriteBtn');
            const copyLinkBtn = document.getElementById('copyLinkBtn');
            const cartToast = new bootstrap.Toast(document.getElementById('cartToast'));

            // 数量增减功能
            decreaseBtn.addEventListener('click', function() {
                let currentValue = parseInt(quantityInput.value);
                if (currentValue > 1) {
                    quantityInput.value = currentValue - 1;
                }
            });

            increaseBtn.addEventListener('click', function() {
                let currentValue = parseInt(quantityInput.value);
                if (currentValue < maxQuantity) {
                    quantityInput.value = currentValue + 1;
                }
            });

            quantityInput.addEventListener('change', function() {
                let currentValue = parseInt(quantityInput.value);
                if (isNaN(currentValue) || currentValue < 1) {
                    quantityInput.value = 1;
                } else if (currentValue > maxQuantity) {
                    quantityInput.value = maxQuantity;
                }
            });

            // 加入购物车功能
            addToCartBtn.addEventListener('click', function() {
                const productId = getProductIdFromUrl();
                const quantity = parseInt(quantityInput.value);
                
                // 这里应该调用后端API将商品加入购物车
                // 暂时使用模拟数据
                console.log('加入购物车:', { productId, quantity });
                
                // 显示成功提示
                cartToast.show();
            });

            // 立即购买功能
            buyNowBtn.addEventListener('click', function() {
                const productId = getProductIdFromUrl();
                const quantity = parseInt(quantityInput.value);
                
                // 这里应该跳转到结算页面
                // 暂时使用模拟数据
                console.log('立即购买:', { productId, quantity });
                
                // 模拟跳转到结算页面
                alert('即将跳转到结算页面');
            });

            // 收藏功能
            favoriteBtn.addEventListener('click', function() {
                this.classList.toggle('active');
                const isActive = this.classList.contains('active');
                const productId = getProductIdFromUrl();
                
                // 这里应该调用后端API收藏/取消收藏商品
                // 暂时使用模拟数据
                console.log('收藏商品:', { productId, isActive });
                
                // 更新按钮文本
                if (isActive) {
                    this.innerHTML = '<i class="bi bi-heart-fill me-2"></i>已收藏';
                } else {
                    this.innerHTML = '<i class="bi bi-heart me-2"></i>收藏';
                }
            });

            // 复制链接功能
            copyLinkBtn.addEventListener('click', function() {
                const url = window.location.href;
                
                // 使用现代API复制链接到剪贴板
                navigator.clipboard.writeText(url).then(function() {
                    // 显示复制成功提示
                    const originalTitle = copyLinkBtn.getAttribute('title');
                    copyLinkBtn.setAttribute('title', '已复制到剪贴板');
                    
                    // 2秒后恢复原始提示文本
                    setTimeout(function() {
                        copyLinkBtn.setAttribute('title', originalTitle);
                    }, 2000);
                }).catch(function(err) {
                    console.error('复制链接失败:', err);
                    alert('复制链接失败，请手动复制链接');
                });
            });

            // 分享功能
            document.querySelectorAll('.share-button').forEach(button => {
                if (button.id === 'copyLinkBtn') return; // 跳过复制链接按钮
                
                button.addEventListener('click', function() {
                    const url = encodeURIComponent(window.location.href);
                    const title = encodeURIComponent(document.title);
                    const imageUrl = encodeURIComponent(document.querySelector('.product-main-image').src);
                    
                    let shareUrl = '';
                    
                    if (this.classList.contains('share-wechat')) {
                        // 微信分享需要生成二维码，这里简化处理
                        alert('请使用微信扫一扫功能扫描二维码分享');
                    } else if (this.classList.contains('share-weibo')) {
                        shareUrl = `http://service.weibo.com/share/share.php?url=${url}&title=${title}&pic=${imageUrl}`;
                        window.open(shareUrl, '_blank');
                    } else if (this.classList.contains('share-qq')) {
                        shareUrl = `https://connect.qq.com/widget/shareqq/index.html?url=${url}&title=${title}&pics=${imageUrl}`;
                        window.open(shareUrl, '_blank');
                    }
                });
            });

            // 获取商品ID的辅助函数
            function getProductIdFromUrl() {
                const pathParts = window.location.pathname.split('/');
                return pathParts[pathParts.length - 1];
            }
        });
    </script>
</body>
</html>